package com.example.administrator.xinyutg.task;

import android.os.AsyncTask;

import com.example.administrator.xinyutg.bean.AdBean;
import com.example.administrator.xinyutg.utils.HttpUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by Administrator on 2015/9/27.
 */
public class AdTask extends AsyncTask<String,Void,String> {
    private List<AdBean> list;
    private List<AdBean> list1;
    private Callback callback;

    public AdTask(List<AdBean> list,List<AdBean> list1,Callback callback) {
        this.list = list;
        this.list1=list1;
        this.callback=callback;
    }

    @Override
    protected String doInBackground(String... params) {
        String str= HttpUtil.getJSON(params[0]);
        return str;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        try {
            JSONObject obj=new JSONObject(s);
            JSONObject object=obj.getJSONObject("data");
            JSONArray array=object.getJSONArray("apptop");
            for (int i = 0; i < array.length(); i++) {
                JSONObject o=array.getJSONObject(i);
                AdBean ad=new AdBean();
                ad.setAction(o.getString("action"));
                ad.setBackground(o.getString("background"));
                ad.setId(o.getString("id"));
                ad.setImage(o.getString("image"));
                list.add(ad);
            }
            JSONArray array1=object.getJSONArray("adinfo");
            for (int i = 0; i < array1.length(); i++) {
                JSONObject o=array1.getJSONObject(i);
                AdBean ad1=new AdBean();
                ad1.setAction(o.getString("action"));
                ad1.setBackground(o.getString("background"));
                ad1.setId(o.getString("id"));
                ad1.setImage(o.getString("image"));
                list1.add(ad1);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        callback.success();
    }
}
